package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ShenMin
 * @date 2021/7/15 11:36
 * @description
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    BrandService brandService;

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(this.brandService.listBrandByIds(ids));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandByCategoryId(@RequestParam("id") Integer categoryId) {
        return ResponseEntity.ok(this.brandService.listBrandByCategoryId(categoryId));
    }

    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> listBrandByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "rows", required = false, defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
    ) {
        return ResponseEntity.ok(this.brandService.listBrandByPage(page, rows, key));
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO,
            @RequestParam("categoryIds") List<Long> cids
    ) {
        this.brandService.addBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO,
            @RequestParam("categoryIds") List<Long> cids
    ) {
        this.brandService.updateBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        this.brandService.deleteBrand(id);
        return ResponseEntity.ok().build();
    }
}
